package com.hibikiluler.javalearning.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hibikiluler.javalearning.entity.Book;
import com.hibikiluler.javalearning.entity.BookType;
import com.hibikiluler.javalearning.mapper.BookMapper;
import com.hibikiluler.javalearning.mapper.BookTypeMapper;
import com.hibikiluler.javalearning.service.BookService;
import com.hibikiluler.javalearning.util.PageResult;
import com.hibikiluler.javalearning.util.Queryinfo;
import com.hibikiluler.javalearning.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @createTime 2021年06月14日 19:08:00
 */
@Service
@Slf4j

public class BookServiceImpl implements BookService {

    @Autowired
    private BookTypeMapper bookTypeMapper;

    @Autowired
    private BookMapper bookMapper;

    @Override
    public Result delete(Long id) {
        bookMapper.delete(id);
        return Result.success("书籍删除成功");
    }

    @Override
    public Result update(Book book) {
        bookMapper.update(book);
        return Result.success("书籍修改成功");
    }

    @Override
    public Result insert(Book book) {
        bookMapper.insert(book);
        return Result.success("书籍添加成功");
    }

    @Override
    public Result findPage(Queryinfo queryInfo) {
        PageHelper.startPage(queryInfo.getPagenumber(), queryInfo.getPagesize());
        Page<BookType> page = bookTypeMapper.findPage(queryInfo.getQueryString());
        return PageResult.pageResult(page.getTotal(), page.getResult());
    }

    @Override
    public Result findBookPage(Queryinfo queryInfo) {
        PageHelper.startPage(queryInfo.getPagenumber(), queryInfo.getPagesize());
        Page<Book> page = bookMapper.findPage(queryInfo.getQueryString());
        return PageResult.pageResult(page.getTotal(), page.getResult());
    }

    @Override
    public Result updateType(BookType bookType) {
        bookTypeMapper.update(bookType);
        return Result.success("书籍分类更新成功");
    }

    @Override
    public Result deleteType(Long id) {
        bookTypeMapper.delete(id);
        return Result.success("书籍分类删除成功");
    }

    @Override
    public Result insertType(BookType bookType) {
        bookTypeMapper.insert(bookType);
        return Result.success("书籍分类添加成功");
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Result batchImport(List<Book> list) {
        List<Book> books = new ArrayList<>();
        //1. 读取分类
        for (Book book : list) {
            BookType bookType = bookTypeMapper.findByTitle(book.getTypeTitle());
            Book title = bookMapper.findByTitle(book.getTitle());
            if (null != bookType) {
                //修改
                if (title != null) {
                    bookMapper.update(book);
                } else {
                    //添加
                    book.setTypeId(bookType.getId());
                    books.add(book);
                }
            }
        }
        bookMapper.insertList(books);
        return Result.success("批量导入成功！");
    }

    @Override
    public Result typeAll() {
        return Result.success("分类查询成功！", bookTypeMapper.typeAll());
    }

    @Override
    public Result findBookByTypeId(Queryinfo queryInfo) {
        PageHelper.startPage(queryInfo.getPagenumber(), queryInfo.getPagesize());
        String queryString = queryInfo.getQueryString();
        Page<Book> books = bookMapper.findByTypeId(queryString);
        return Result.success("书籍查询成功！", new PageResult(books.getTotal(), books.getResult()));
    }

    @Override
    public Result findMiniPage(JSONObject object) {
        PageHelper.startPage(object.getInteger("pagenumber"), object.getInteger("pagesize"));
        Long typeId = object.getLong("typeId");
        String keywords = object.getString("keywords");
        Page<Book> books = bookMapper.findMiniPage(typeId, keywords);
        return PageResult.pageResult(books.getTotal(), books.getResult());
    }

    @Override
    public Result findById(Long id) {
        if (id == null) {
            return Result.fail("请传递书籍编号");
        }
        return Result.success("书籍信息查询成功", bookMapper.findById(id));
    }
}

